package com.lazydong.apiforward.client.route;

import com.lazydong.apiforward.client.global.GlobalData;
import com.lazydong.apiforward.client.gui.pages.userproxy.UserProxy;
import com.lazydong.apiforward.gateway.filter.CustomGlobalFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;

/**
 * 请求头处理器
 */
public class UserProxyFilter implements CustomGlobalFilter {
    @Override
    public void beforeRequest(ServerHttpRequest request, Route route) {

    }

    @Override
    public void afterResponse(ServerHttpResponse response) {

    }

    @Override
    public void webExchange(ServerWebExchange exchange) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder mutate = request.mutate();
        //删除对应的内容
//        mutate.headers(e -> e.remove(HttpHeaders.AUTHORIZATION));
        mutate.headers(e -> e.remove("想要删除的header"));
        //这个是新增或者修改
        UserProxy userProxy = GlobalData.USER_PROXY.get();
        if (userProxy != null) {
            mutate.header("userToken", userProxy.token);
        }
        ServerHttpRequest build = mutate.build();
        exchange.mutate().request(build).build();
    }
}
